package com.xiaohao.news.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaohao.news.common.R;
import com.xiaohao.news.entity.User;
import com.xiaohao.news.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param
     * @param
     * @return
     */
    @PostMapping("/login")
    public R<User> login(HttpServletRequest request, @RequestBody User user) {

        //1、将页面提交的密码password进行md5加密处理
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        log.info("用户信息：{}", user);

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        User user1 = userService.getOne(queryWrapper);

        // 3、如果没有查询到则返回登陆失败结果
        if (user1 == null) {
            return R.error("登录失败，没有此用户");
        }

        //4、密码对比，如果不一致则返回登陆失败结果
        if (!user1.getPassword().equals(password)) {
            return R.error("登陆失败,密码错误");
        }

        //5、登陆成功，将用户id存入Session 并 返回登陆成功结果
        request.getSession().setAttribute("user", user1.getId());
        return R.success(user1);
    }

    /**
     * 用户退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的的当前登录用户的id
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }

    /**
     * 注册账号
     * @param user
     * @return
     */

    @PostMapping("/register")
    public R<String> register(@RequestBody User user) {
        log.info("用户注册，用户信息：{}", user.toString());

        String username = user.getUsername();
        String password = user.getPassword();

        //1.检查用户名是否重复
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        if (userService.getOne(wrapper) != null) {//若存在，则注册失败，返回相应提示
            return R.error("用户名重复");
        }

        //2.设置初始密码，需要进行md5加密处理
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        user.setRole("1");
        System.out.println(user.getPassword());
        userService.save(user);

        return R.success("注册成功");
    }

    /**
     * 返回用户信息
     * @return
     */
    @GetMapping()
    public R<User> info(@RequestBody User user) {
        log.info("获取用户信息，目标用户id为", user.getId());
        User user1 = userService.getById(user.getId());
        if (user1 == null) {
            return R.error("查询不到该用户");
        }
        log.info("用户信息：{}", user1);
        return R.success(user1);
    }

    /**
     * 修改员工信息
     * @param request
     *
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody User user) {
        log.info("用户信息：{}",user);
        userService.updateById(user);
        return R.success("修改成功");
    }
}